package com.example.myapplication.fragment;


import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.graphics.Palette;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.example.myapplication.R;

import com.example.myapplication.bean.PaperInformation;
import com.example.myapplication.callback.UserInfoCallBack;
import com.example.myapplication.ic.FastBlurUtil;
import com.example.myapplication.ic.UserInfoStudent;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentStudentInfo extends Fragment implements UserInfoCallBack {

    private TextView user_name;
    private CircularImageView user_img;
    private PaperInformation paperInformation;
    private UserInfoStudent userInfoStudent;
    private BitmapUtils bitmapUtils;
    private LinearLayout linearLayout_info;
    public FragmentStudentInfo() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_student_info,container,false);
        initView(view);
        Bundle bundle = getArguments();
        paperInformation = (PaperInformation) bundle.getSerializable("userinfo");
        userInfoStudent = new UserInfoStudent();
        if (paperInformation != null) {
            String userid =paperInformation.getStudent_number();
            userInfoStudent.getInfo(userid,this);
        }else{
            Snackbar.make(view,"wrong",Snackbar.LENGTH_SHORT).show();
        }

        return view;
    }


    private void initView(View view){
        user_img = (CircularImageView) view.findViewById(R.id.user_img);
        user_name = (TextView) view.findViewById(R.id.user_name);
        linearLayout_info = (LinearLayout) view.findViewById(R.id.linear_info);
    }

    @Override
    public void onSuccess(String info) {
        Log.i("iiiii",info);
        try {
            JSONObject jsonObject = new JSONObject(info);
            JSONObject jsonObject1 = jsonObject.getJSONObject("info");
            String studentId = jsonObject1.getString("student_id");
            String student_name = jsonObject1.getString("student_name");
            String student_number = jsonObject1.getString("student_number");
            String student_pic = jsonObject1.getString("student_pic");
            char a = student_pic.charAt(0);
            char b = student_pic.charAt(1);
            if(a=='.'){
                student_pic = student_pic.substring(1);
            }
            if(b=='.'){
                student_pic = student_pic.substring(2);
            }
            user_name.setText(student_name);
            bitmapUtils = new BitmapUtils(getActivity());

            bitmapUtils.display(user_img,"http://192.168.191.1/app/pic"+student_pic, new BitmapLoadCallBack<CircularImageView>() {
                @Override
                public void onLoadCompleted(CircularImageView circularImageView, String s, final Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {

                    circularImageView.setImageBitmap(bitmap);
                    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                        @Override
                        public void onGenerated(Palette palette) {
                            Palette.Swatch v = palette.getLightMutedSwatch();
                            Log.i("v",v.getRgb()+"");
                            if(v!=null) linearLayout_info.setBackgroundColor(v.getRgb());
                            linearLayout_info.buildDrawingCache();
                            linearLayout_info.setDrawingCacheEnabled(true);
                            Bitmap bitmap1 =   linearLayout_info.getDrawingCache();
                            int scaleRatio = 10;
                            int blurRadius = 8;
                            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap1,
                                    bitmap1.getWidth() / scaleRatio,
                                    bitmap1.getHeight() / scaleRatio,
                                    false);
                            Bitmap blurBitmap = FastBlurUtil.doBlur(scaledBitmap, blurRadius, true);
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                linearLayout_info.setBackground(new BitmapDrawable(blurBitmap));
                            }
                        }
                    });

                }

                @Override
                public void onLoadFailed(CircularImageView circularImageView, String s, Drawable drawable) {
                    Log.i("jsjs","falie");
                }
            });


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFail() {

    }


}
